using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;


namespace FiltronASP.Helpers.AjaxModel
{
    /// <summary>
    /// Klasa pomocnicza do generowania stringa z lista modeli.
    /// </summary>
    public static class ApplHelperFiltron
    {

        public static string GenerateFilterDropDown(int typeId)
        {
            string comboString = "";
            comboString += "--- wybierz model ---, -1;";
            IFiltronFilterDAO FiltronFilterDAO = new FiltronFilterDAO();
            if (typeId > 0)
            {
                IQueryable<FiltronFilter> filtersCollection = FiltronFilterDAO.FindByType(typeId);
                foreach (FiltronFilter filter in filtersCollection)
                {
                    comboString += filter.filter_number;
                    comboString += ",";
                    comboString += filter.filter_id;
                    comboString += ";";
                }   //foreach
            }   //if
            return comboString;
        }

        public static string GenerateModelDropDown(int filterId)
        {
            string comboString = "";
            string MakeObjIds = String.Empty;
            comboString += "--- wybierz model ---, -1;";
            IFiltronFilterDAO FiltronFilterDAO = new FiltronFilterDAO();
            if (filterId > 0)
            {
                IFiltronFilterEngineDAO filtronFilterEnginesDAO = new FiltronFilterEngineDAO();
                IQueryable<FiltronFilterEngine> filtronFilterEngine = filtronFilterEnginesDAO.FindByFilter(filterId);
                foreach (FiltronFilterEngine filtronFilterEngineSingle in filtronFilterEngine)
                {
                    int modelId = Convert.ToInt32(filtronFilterEngineSingle.model_id);
                    IFiltronModelDAO FiltronModelDAO = new FiltronModelDAO();
                    FiltronModel ModelObject = FiltronModelDAO.FindByPK(modelId);
                    int makeId = Convert.ToInt32(ModelObject.make_id);
                    IFiltronMakeDAO FiltronMakeDAO = new FiltronMakeDAO();
                    FiltronMake MakeObject = FiltronMakeDAO.FindByPK(makeId);

                    
                    
                    string[] splitedIds = MakeObjIds.Split(",".ToCharArray());
                    List<string> arr = new List<string>(splitedIds);
                    if (arr.Contains(Convert.ToString(MakeObject.make_id)))
                    {
                        // nth
                    }
                    else {
                        comboString += MakeObject.make_name;
                        comboString += ",";
                        comboString += MakeObject.make_id;
                        comboString += ";";
                        MakeObjIds += MakeObject.make_id + ",";
                    }

                }
            }   //if
            return comboString;
        }

        public static string GenerateTypeDropDown(int filterId, int makeId)
        {
            string comboString = String.Empty;
            string modelsId = String.Empty;

            comboString += "--- wybierz model ---, -1;";
            IFiltronFilterEngineDAO FiltronFilterEngineDAO = new FiltronFilterEngineDAO();
            IQueryable<FiltronFilterEngine> FilterEngineCollection = FiltronFilterEngineDAO.FindByFilter(filterId);
            if (FilterEngineCollection != null) {
                foreach (FiltronFilterEngine filter in FilterEngineCollection) {
                    int modelId = Convert.ToInt32(filter.model_id);
                    string[] splitedIds = modelsId.Split(",".ToCharArray());
                    List<string> arr = new List<string>(splitedIds);
                    if (modelId > 0) {
                        if (arr.Contains(Convert.ToString(modelId))) {
                            /// nth
                        } else {
                            IFiltronModelDAO FiltronModelsDAO = new FiltronModelDAO();
                            FiltronModel FiltronModel = FiltronModelsDAO.FindByPK(modelId);
                            if (FiltronModel != null) {
                                if (FiltronModel.make_id.Equals(makeId)) {
                                    comboString += FiltronModel.model_name;
                                    comboString += ",";
                                    comboString += FiltronModel.model_id;
                                    comboString += ";";
                                    modelsId += modelId + ",";
                                }
                            }
                        }
                    }
                }
            }

            return comboString;
        }

        public static string GenerateEngines(int filterId, int modelId)
        {
            string htmlToReturn = String.Empty;

            IFiltronFilterEngineDAO FiltronFilterEngineDAO = new FiltronFilterEngineDAO();
            IQueryable<FiltronFilterEngine> FilterEngineCollection = FiltronFilterEngineDAO.FindByFilter(filterId);

            htmlToReturn += "            <h3>Silniki</h3>";
            htmlToReturn += "            <table>";
            htmlToReturn += "                <thead>";
            htmlToReturn += "                    <td class=\"engine\">Silnik</td>";
            htmlToReturn += "                    <td class=\"cc\">CC <img src=\"../../../Content/images/engine_cc.jpg\" alt=\"cc\"></td>";
            htmlToReturn += "                    <td class=\"no\">No <img src=\"../../../Content/images/engine_no.jpg\" alt=\"no\"></td>";
            htmlToReturn += "                    <td class=\"hp\">HP <img src=\"../../../Content/images/engine_hp.jpg\" alt=\"hp\"></td>";
            htmlToReturn += "                </thead>";
            htmlToReturn += "                <tbody>";

            foreach (FiltronFilterEngine filterEngine in FilterEngineCollection) {
                if (filterEngine.model_id.Equals(modelId)) {
                    int engineId = filterEngine.engine_id;
                    IFiltronEngineDAO  filtronEngineDAO = new FiltronEngineDAO();
                    FiltronEngine engineSingle = filtronEngineDAO.FindByPK(engineId);

                    htmlToReturn += String.Format("<tr>", engineSingle.engine_id);
                    htmlToReturn += String.Format("<td>{0}</td>", engineSingle.engine_name);
                    htmlToReturn += String.Format("<td>{0}</td>", engineSingle.engine_cubic_capacity);
                    htmlToReturn += String.Format("<td>{0}</td>", engineSingle.engine_code);
                    htmlToReturn += String.Format("<td>{0}</td>", engineSingle.engine_horse_power);
                    htmlToReturn += "</tr>";

                }
            }
            htmlToReturn += "                </tbody>";
            htmlToReturn += "            </table>";

            return htmlToReturn;
        }
    }
}
